<?php
class SucculentHarvestAction extends SzAbstractAction
{
    protected $paramTypes = array(
        SzAbstractAction::TYPE_INT,
        SzAbstractAction::TYPE_ARRAY,
        SzAbstractAction::TYPE_INT
    );

    /**
     * @param $userId
     * @param $pos
     * @param $reqTime
     * @return SzResponse
     * @throws SzException
     */
    public function execute($userId, $pos, $reqTime)
    {
        GameService::get()->validateMultiRequestTimeDelta($userId, $reqTime);

        //获取玩家周年庆buff
        /**
         * @var ModuleProfileInfoVoList $profileInfoList
         * @var ModuleProfileInfoVo $profileInfoFlower
         * @var ModuleProfileInfoVo $profileInfoSeed
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfoFlower = $profileInfoList->getElement(GameConst::USER_ANNIVERSARY_GET_FLOWER, false);
        $profileInfoSeed = $profileInfoList->getElement(GameConst::USER_ANNIVERSARY_GET_SEED, false);
        $hasBuffFlower = false;
        if ($profileInfoFlower){
            if ($profileInfoFlower->getInfo() > $reqTime){
                $hasBuffFlower = true;
            }
        }
        $hasBuffSeed = false;
        if ($profileInfoSeed){
            if ($profileInfoSeed->getInfo() > $reqTime){
                $hasBuffSeed = true;
            }
        }


        $plantList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_SUCCULENT_PLANT_INFO);
        $returnArr = array();
        $num = 0;
        foreach ($pos as $position){
            List($seedId ,$plant,$count,$item,$isDrop,$acNum) = SucculentService::get()->harvest($userId, $position, $reqTime,$plantList,$hasBuffFlower,$hasBuffSeed,$profileInfoList);
            if (empty($seedId)){
                continue;
            }
            else{
                if (!isset($returnArr[$position])){
                    $returnArr[$position] = array(
                        'plant' => $plant->toArray(),
                        'count' => $count,
                    );
                    if ($isDrop) {
                        $returnArr[$position]['item'] = $item;
                    }
                }
                else{
                    $returnArr[$position]['count'] += $count;
                    if (!empty($item[0])){
                        $returnArr[$position]['item'][$item[0]] += $item[1];
                    }
                }
                $returnArr[$position]['actNum'] = $acNum;
                $num++;
            }
        }



        return $this->buildResponse($returnArr);
    }
}